<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('user.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('user.username')" prop="username">
            <el-input v-model="dataForm.username" :placeholder="i18n('user.please_mobile')"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('common.default_password')">
            <el-tag type="danger">123456</el-tag>
          </el-form-item>

          <el-form-item prop="role_id" :label="i18n('user.role.title')" class="width_auto">
            <el-radio-group v-model="dataForm.role_id">
              <el-radio v-for="(v, k) in rolelist" :key="k" :label="v.id">
                {{ v.title }}
              </el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item :label="i18n('user.nickname')" prop="nickname">
            <el-input v-model="dataForm.nickname" :placeholder="i18n('common.please_input') + i18n('user.nickname')"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('user.avatar')" prop="avatar">
            <el-upload
              class="avatar-uploader"
              :action="$http.adornUrl('/file/picture')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-success="handleAvatarSuccess"
              :before-upload="beforePictureUpload">
              <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar-upload"/>
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item :label="i18n('user.mobile')" prop="mobile">
            <el-input v-model="dataForm.mobile" :placeholder="i18n('common.please_input') + i18n('user.mobile')"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('user.email')" prop="email">
            <el-input v-model="dataForm.email" :placeholder="i18n('common.please_input') + i18n('user.email')"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('kernel:user:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="user-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useStore } from "@/hooks/store/useStore"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { user } = useStore()
  const { router, query, i18n, isAuth } = useBase()

  const model = ref('user')
  const rolelist = ref([])
  const dataForm = reactive({
    id: 0,
    role_id: '',
    username: '',
    nickname: '',
    avatar: '',
    mobile: '',
    email: '',
  })

  const dataRule = reactive({
    username: [{
      required: true,
      message: i18n('user.rules.username.require'),
      trigger: 'blur',
    }],
    nickname: [{
      required: true,
      message: i18n('user.rules.nickname.require'),
      trigger: 'blur',
    }],
    role_id: [{
      required: true,
      message: i18n('user.rules.role.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handleAvatarSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/user/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.role_id = data.data.role_id
            dataForm.username = data.data.username
            dataForm.nickname = data.data.nickname
            dataForm.avatar = data.data.avatar
            dataForm.mobile = data.data.mobile
            dataForm.email = data.data.email
          }
        })
      }
    })
  }

  const loadRoleList = () => {
    $http({
      url: $http.adornUrl('/role/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        rolelist.value = data.data
      } else {
        this.$message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    init(formRef)
    loadRoleList()
  })
</script>
